<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath());
    %>
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</head>
<body>
<jsp:include page="admin_header.jsp"/>

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>管理论文</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 col-md-offset-8">
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table class="table table-hover" id="managerPaper_table">
                <thead>
                <tr>
                    <th>刊物编码</th>
                    <th>论文名称</th>
                    <th>论文类型</th>
                    <th>第一作者</th>
                    <th>其他作者</th>
                    <th>发表时间</th>
                    <th>支持基金</th>
                    <th>当前状态</th>
                    <th>成果分值</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>

                </tbody>

            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6" id="page_info_area">
        </div>
        <div class="col-md-6" id="page_nav_area">
        </div>
    </div>
</div>
<script type="text/javascript">

    //1、页面加载完成以后，直接去发送一个ajax请求，要到分页数据
    $(function () {
        //去首页
        to_page(1, "/selCheckAndRemovePaper");
    });

    function to_page(pn, road) {
        $.ajax({
            url: "${APP_PATH}" + road,
            data: "pn=" + pn,
            type: "POST",
            success: function (result) {
                //1、解析并显示数据
                build_check_paper(result);
                //2、解析并显示分页数据
                build_page_info(result);
                //3、解析显示分页条数据
                build_page_nav(result, road);
            }
        });
    }

    //1、解析并显示数据
    function build_check_paper(result) {
        //清空table表格
        $("#managerPaper_table tbody").empty();
        var emps = result.data.list;
        $.each(emps, function (index, item) {
            var resultUUIdTd = $("<td></td>").append(item.resultuuid);
            var infor = item.papername;
            var a_left = "<a target = '_blank' href='C:\\Users\\Administrator\\Documents\\Tencent Files\\370216714\\FileRecv\\teacher_science_system\\target\\ScientificInfo\\upload\\" + item.papername + ".doc'>";
            var a_right = "</a>";
            var paperTypeId = $("<td></td>").append(item.papertypeid);
            var paperNameTd = $("<td></td>").append(a_left + infor + a_right);
            var firstNameTd = $("<td></td>").append(item.firstname);
            var allNameTd = $("<td></td>").append(item.allname);
            var endTimeTd = $("<td></td>").append(item.endtime);
            var supportFunTd = $("<td></td>").append(item.supportfun);
            var stateTd = $("<td></td>");
            var scoreData = $("<td></td>").append(item.score);

            if (item.checkflag == 1) {
                stateTd.append("审核通过");
            } else if (item.checkflag == 3) {
                stateTd.append("已被下架");
            } else if (item.checkflag == 0) {
                stateTd.append("未经过审核");
            }
            var subBtn = $("<button></button>").addClass("btn btn-success btn-sm sub_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-ok"))
                .append("通过");
            subBtn.attr("id", item.resultuuid);

            var failBtn = $("<button style='margin-top:4px;margin-left:4px;'></button>").addClass("btn btn-danger btn-sm fail_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-pencil")
                    .append("下架"));

            failBtn.attr("id", item.resultuuid);

            console.log(item.papername)
            $.ajax({
                url: "${APP_PATH}/isExistsFilename",
                data: {"paperName": item.papername},
                type: "GET",
                dataType: "JSON",
                success: function (result) {
                    //数据库存储了文件信息的论文 才可以下载
                    if(result.data == false){
                        $("." + item.papername).remove();
                    }
                },
                error: function (error) {
                    console.log(error)
                }
            });

            var download = $("<a style='margin-top:4px;margin-left:4px;'></a>").addClass( item.papername + " btn btn-danger btn-sm").append($("<span></span>").addClass("glyphicon glyphicon-download")).append("下载").attr("href", "/DownloadServlet?paperName=" + item.papername);

            //评分功能
            var score = $("<button style='margin-top:4px;margin-left:4px;'></button>").addClass("btn btn-danger btn-sm score_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-ok")
                    .append("评分"));
            score.attr("name", item.papername);

            var view = $("<button style='margin-top: 0px;'></button>").addClass("btn btn-danger btn-sm view_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-eye-open")
                    .append("预览"));
            view.attr("id", item.resultuuid);
            var view1 = $("<td></td>").append(view);


            if (item.checkflag == 3 || item.checkflag == 0) {
                var btnTd = $("<td></td>").append(subBtn);
                $("<tr></tr>").append(resultUUIdTd).append(paperNameTd)
                    .append(firstNameTd).append(allNameTd)
                    .append(endTimeTd).append(supportFunTd)
                    .append(stateTd).append(btnTd)
                    .appendTo("#managerPaper_table tbody");
            } else if (item.checkflag == 1) {  //审核通过
                var btnTd = $("<td class='btnTd' ></td>").append(failBtn).append(score).append(download);
                $("<tr></tr>").append(resultUUIdTd).append(paperNameTd).append(paperTypeId)
                    .append(firstNameTd).append(allNameTd)
                    .append(endTimeTd).append(supportFunTd)
                    .append(stateTd).append(scoreData).append(btnTd)
                    .appendTo("#managerPaper_table tbody");
            }
        });
    }

    //解析显示分页信息
    function build_page_info(result) {
        $("#page_info_area").empty();
        $("#page_info_area").append("当前第" + result.data.pageNum +
            "页，总共" + result.data.pages + "页，总共" + result.data.total + "条记录。");

    }

    //解析显示分页条，点击分页要能去下一页
    function build_page_nav(result, road) {
        $("#page_nav_area").empty();
        var ul = $("<ul></ul>").addClass("pagination");
        //构建元素
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href", "#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
        if (result.data.hasPreviousPage == false) {
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");

        } else {
            //为元素添加点击翻页的事件
            firstPageLi.click(function () {
                to_page(1, road);
            });

        }
        prePageLi.click(function () {
            to_page(result.data.pageNum - 1, road);
        });
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href", "#"));
        if (result.data.hasNextPage == false) {
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        } else {
            nextPageLi.click(function () {
                to_page(result.data.pageNum + 1, road);
            });
            lastPageLi.click(function () {
                to_page(result.data.pages, road);
            });
        }


        //添加首页和前一页的提示
        ul.append(firstPageLi).append(prePageLi);
        //1,2,3,4,5,遍历给ul中添加页码提示
        $.each(result.data.navigatepageNums, function (index, item) {

            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if (result.data.pageNum == item) {
                numLi.addClass("active");
            }
            numLi.click(function () {
                to_page(item, road);
            });
            ul.append(numLi);
        });
        //添加下一页和末页的提示
        ul.append(nextPageLi).append(lastPageLi);
        //把ul加入到nav
        var navEle = $("<nav></nav>").append(ul);
        navEle.appendTo("#page_nav_area");
    }


    //通过审核
    $(document).on("click", ".sub_btn", function () {
        // var val = $(this).attr("id");
        check($(this).attr("id"), 1, "/reUpdatePaper");

    });

    //下架
    $(document).on("click", ".fail_btn", function () {
        check($(this).attr("id"), 3, "/reUpdatePaper");
    });

    // 下载
    // download_btn
    // $(document).on("click", ".download_btn", function () {
    //     var paperName = $(this).attr("name");
    //     alert("鼠标移动到相应“论文名称”，右键，点击复制链接地址。粘贴到浏览器地址栏中，打开文件...");
    // });

    //评分
    $(document).on("click", ".score_btn", function () {
        // 拿到该行的paperName
        var paperName = $(this).attr("name");
        var score = prompt('请输入您评的分数：')
        if (score == null) {
        } else {
            scoreFun(score, paperName, "/score");
        }
    });

    function scoreFun(score, name, road) {
        $.ajax({
            url: "${APP_PATH}" + road,
            data: {"score": score, "name": name},
            type: "POST",
            dataType: "JSON",
            success: function (result) {
                alert(result.message);
                to_page(1, "/selCheckAndRemovePaper");
            }
        });
    }

    //预览
    // view_btn
    $(document).on("click", ".view_btn", function () {
        view_check($(this).attr("id"), "/view");
    });


    function download_check(paperId, road) {
        $.ajax({
            url: "${APP_PATH}" + road,
            data: {"paperId": paperId},
            type: "GET",
            dataType: "JSON",
            success: function (result) {
                alert(result.message);
                to_page(1, "/selCheckAndRemovePaper");
            }
        });
    }

    function view_check(paperId, road) {
        $.ajax({
            url: "${APP_PATH}" + road,
            data: {"paperId": paperId},
            type: "GET",
            dataType: "JSON",
            success: function (result) {
                alert(result.message);
                to_page(1, "/selCheckAndRemovePaper");
            }
        });
    }

    function check(paperId, opeCode, road) {
        $.ajax({
            url: "${APP_PATH}" + road,
            data: {"paperId": paperId + "", "operaCode": opeCode},
            type: "GET",
            success: function () {
                alert("操作成功！")
                to_page(1, "/selCheckAndRemovePaper");
            }
        });
    }

</script>

<jsp:include page="admin_foot.jsp"/>
</body>
</html>
